{extend name="index_layout"/}
{block name="main"}
<link href="/static/libs/formSelects-v4.css" rel="stylesheet" />
<script src="/static/libs/formSelects-v4.js"></script>
<form class="layui-form" method="post">
    
    <div class="layui-form-item">
        <label class="layui-form-label">工序编号 </label>
        <div class="layui-input-block">
            <input type="text" name="nums" value="{$procedure.nums}" lay-verify="required" autocomplete="off" placeholder="工序编号" class="layui-input layui-disabled">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">工序名称 </label>
        <div class="layui-input-block">
            <input type="text" name="title" value="{$procedure.title}" lay-verify="required" autocomplete="off" placeholder="工序名称" class="layui-input">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">报工权限 </label>
        <div class="layui-input-block">
            <select name="auth" id="" xm-select-search=""  xm-select="userDetailOrganizationSelectId">
                <option value="0">所有人</option>
                {foreach name="userList" id="item"}
                <option selected value="{$item.id}">{$item.nickname}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">报工配数比 </label>
        <div class="layui-input-block">
            <input type="text" name="ratio" value="{$procedure.ratio}" lay-verify="required" autocomplete="off" placeholder="工序名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">不良品列表 </label>
        <div class="layui-input-block">
            <select name="bad_ids" id="" xm-select-search="" lay-verify="required"  xm-select="userDetailOrganizationSelectId2">
                {foreach name="badList" id="item"}
                <option value="{$item.id}">{$item.title}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <input type="hidden" name="id" value="{$procedure.id}">
    <div class="layui-form-item">
        <label class="layui-form-label">工序采集数据 </label>
        <div class="layui-input-block">
            <table class="layui-table">
                <thead>
                    <th>类型</th>
                    <th>名称</th>
                    <th></th>
                </thead>
                <tbody>
                    {volist name="procedure.data_list" id="item" key="k"}
                    <tr>
                        <td>
                            <select name="type[]" id="type">
                                <option {if $key=='text'} selected {/if} value="text">文本</option>
                                <option {if $key=='number'} selected {/if} value="number">数字</option>
                                <option {if $key=='time'} selected {/if} value="time">时间</option>
                                <option {if $key=='photo'} selected {/if} value="photo">照片</option>
                            </select>
                        </td>
                        <td>
                            <input type="text" name="name[]" value="{$item}">
                        </td>
                        <td>
                            
                            {if $k>1}
                            <button class="layui-btn layui-btn-xs layui-btn-danger del">删除</button>
                            {/if}
                        </td>
                    </tr>
                    {/volist}
                </tbody>
            </table>
            
            <button class="layui-btn layui-btn-xs add-line" type="button" style="margin-left: 20%;">增加</button>
        </div>
    </div>
    {volist name="custom" id="item" key="k"}
        {switch item.field_type}
            {case text}
                <div class="layui-form-item" id="extra_{$k}">
                    <label class="layui-form-label">{if strpos($item.attribute.attribute,'2') !== false} <span class="bitian">*</span>{/if} {$item.title} </label>
                    <div class="layui-input-block">
                        <input type="hidden" name="extra-title[]" value="{$item.title}">
                        <input type="hidden" name="field_type[]" value="{$item.field_type}">
                        <input type="text" name="extra_{$k}" value="{$item.value}" {if strpos($item.attribute.attribute,'2') !== false} lay-verify="required" {/if} autocomplete="off" placeholder="{$item.attribute.placeholder}" class="layui-input {if strpos($item.attribute.attribute,'1') !== false} layui-disabled {/if}">
                    </div>
                </div>
            {/case}
            {case number}
                <div class="layui-form-item" id="extra_{$k}">
                    <label class="layui-form-label">{if strpos($item.attribute.attribute,'2') !== false} <span class="bitian">*</span>{/if} {$item.title} </label>
                    <div class="layui-input-block">
                        <input type="hidden" name="extra-title[]" value="{$item.title}">
                        <input type="hidden" name="field_type[]" value="{$item.field_type}">
                        <input type="text" name="extra_{$k}" value="{$item.value}" {if strpos($item.attribute.attribute,'2') !== false} lay-verify="required" {/if} autocomplete="off" placeholder="{$item.attribute.placeholder}" class="layui-input {if strpos($item.attribute.attribute,'1') !== false} layui-disabled {/if}" {if strpos($item.attribute.attribute,'4') !== false} onclick="saveXiaoshu(this,'{$item.attribute.xiaoshu}')"{/if} {if strpos($item.attribute.attribute,'3') !== false} qian="1" onkeyup="SnumFormat(this)" {/if} >
                    </div>
                </div>
            {/case}
            {case time}
                <div class="layui-form-item" id="extra_{$k}">
                    <label class="layui-form-label">{if strpos($item.attribute.attribute,'2') !== false} <span class="bitian">*</span>{/if} {$item.title} </label>
                    <div class="layui-input-block">
                        <input type="hidden" name="extra-title[]" value="{$item.title}">
                        <input type="hidden" name="field_type[]" value="{$item.field_type}">
                        <input type="text" name="extra_{$k}" value="{$item.value}" id="test1" class="layui-input">
                    </div>
                </div>
            {/case}
            {case radio}
                <div class="layui-form-item" id="extra_{$k}">
                    <label class="layui-form-label">{if strpos($item.attribute.attribute,'1') !== false} <span class="bitian">*</span>{/if} {$item.title} </label>
                    <div class="layui-input-block">
                        <input type="hidden" name="extra-title[]" value="{$item.title}">
                        <input type="hidden" name="field_type[]" value="{$item.field_type}">
                        <select name="extra_{$k}" id="" lay-filter="changeValue">
                            {volist name="item.attribute.select" id="select"}
                            <option {if $item.value == $select} selected {/if} value="{$select}">{$select}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
            {/case}
            {case checkbox}
                <div class="layui-form-item" id="extra_{$k}">
                    <label class="layui-form-label">{if strpos($item.attribute.attribute,'1') !== false} <span class="bitian">*</span>{/if} {$item.title} </label>
                    <div class="layui-input-block">
                        <input type="hidden" name="extra-title[]" value="{$item.title}">
                        <input type="hidden" name="field_type[]" value="{$item.field_type}">
                        <select {if strpos($item.attribute.attribute,'1') !== false} lay-verify="required" {/if} name="extra_{$k}" id="" key="{$k}" class="mutiple-select" xm-select-search=""  xm-select="userDetailOrganizationSelectId{$k}">
                            {volist name="item.attribute.select" id="select"}
                            <option value="{$select}">{$select}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
            {/case}
        {/switch}
    {/volist}
    
    <div class="layui-form-item layer-footer">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter='ds' type="button" data-refresh="false">立即提交</button>
            <button class="layui-btn layui-btn-normal" type="button" onclick="var index1 = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index1);">返回</button>
        </div>
    </div>
</form>

{/block}

{block name="script"}
<script>
    $(function(){
        layui.formSelects.render('userDetailOrganizationSelectId', {
            skin: "normal", //多选皮肤normal|primary|default|danger|warm
            height: "auto",                //是否固定高度, 38px | auto
            radio: false,                  //是否设置为单选模式
            direction: "down",        //显示方式向下显示
            on: function(id, vals, val, isAdd, isDisabled){
            },            //同formSelects.on
        })
        layui.formSelects.render('userDetailOrganizationSelectId2', {
            skin: "normal", //多选皮肤normal|primary|default|danger|warm
            height: "auto",                //是否固定高度, 38px | auto
            radio: false,                  //是否设置为单选模式
            direction: "down",        //显示方式向下显示
            on: function(id, vals, val, isAdd, isDisabled){
            },            //同formSelects.on
        })
        var auth = JSON.parse('{:json_encode($procedure.auth)}');
        var bad_ids = JSON.parse('{:json_encode($procedure.bad_ids)}');
        console.log(auth);
        console.log(bad_ids);
        layui.formSelects.value('userDetailOrganizationSelectId',auth);
        layui.formSelects.value('userDetailOrganizationSelectId2',bad_ids);
        hideForm();
        var custom = JSON.parse('{:json_encode($custom)}');
        $('.mutiple-select').each(function(){
            var xm_select_id = $(this).attr('xm-select');
            var key = $(this).attr('key');
            var value = custom[key-1].value;
            value = value?value.split(','):[];
            layui.formSelects.render(xm_select_id, {
                skin: "normal", //多选皮肤normal|primary|default|danger|warm
                height: "auto",                //是否固定高度, 38px | auto
                radio: false,                  //是否设置为单选模式
                direction: "down",        //显示方式向下显示
                on: function(id, vals, val, isAdd, isDisabled){
                },            //同formSelects.on
            })
            layui.formSelects.value(xm_select_id,value);
        })
    })
    layui.use(['form','jquery'], function() {
        var form = layui.form;
        var $ = layui.jquery;
        form.on('submit(ds)',function(data){
            var param = data.field;
            $.post('{:url("editPost")}',param,function(res){
                if(res.code > 0){
                    layer.msg('修改成功');
                    setTimeout(() => {
                        var index1 = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index1);
                        parent.location.reload();
                    }, 500);
                }else{
                    layer.msg(res.msg);
                }
            },'json');
        })
        var custom = JSON.parse('{:json_encode($custom)}');
        hiddenList = [];
        for(var i in custom){
            hidden = custom[i].attribute.hidden;
            if(hidden){
                hiddenList.push({'key':i,'hidden':hidden});
            }
        }
        $('.add-line').on('click',function(){
            var _html = '<tr><td>'+
                            '<select name="type[]" id="type">'+
                                '<option value="text">文本</option>'+
                                '<option value="number">数字</option>'+
                                '<option value="time">时间</option>'+
                                '<option value="photo">照片</option>'+
                            '</select>'+
                    '</td>'+
                    '<td>'+
                        '<input type="text" name="name[]">'+
                    '</td>'+
                    '<td><button class="layui-btn layui-btn-xs layui-btn-danger del">删除</button>'+
                    '</td></tr>';
            $('.layui-table').find('tbody').append(_html); 
            form.render();
        })

        $('body').on('click','.del',function(){
            $(this).parent().parent().remove();
        })
    })
</script>
<script src="/static/libs/hide.js"></script>
{/block}